package com.gitee.vueantd.upload.domain;

import com.gitee.vueantd.common.base.BaseEntity;
import com.gitee.vueantd.upload.common.ProcessStatus;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.codec.digest.DigestUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "upload_task_data", indexes = {
        @Index(name = "idx_upload_task_data_file_type", columnList = "file_type"),
        @Index(name = "idx_upload_task_data_task_uuid", columnList = "task_uuid", unique = true),
})
public class UploadTaskData extends BaseEntity implements Serializable {
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Id
  @Column(name = "id")
  @NotNull(groups = {Update.class})
  private Long id;

  @Column(name = "file_type")
  private Integer fileType;

  @Column(name = "name")
  private String name;

  @Column(name = "user_name")
  private String userName;

  @Column(name = "status")
  private ProcessStatus status = ProcessStatus.PENDING;

  @Column(name = "file_path")
  private String filePath;

  @Column(name = "extra_id", length = 60)
  private String extraId;   //不同数据来源的额外id记录, 比如uuid

  @Column(name = "node_id")
  private Long nodeId;

  @Column(name = "task_uuid")
  private String taskUuid;

  @Column(name = "thumbnail_path")
  private String thumbnailPath;

  public String buildTaskUuid() {
    return DigestUtils.md5Hex(this.filePath + "/" + this.nodeId + "/" + this.extraId + "/" + this.fileType);
  }

  public UploadTaskData(Integer fileType, String name) {
    this.fileType = fileType;
    this.name = name;
  }
}
